<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\CurriculumService;
use app\admin\model\User;
use app\admin\model\School;
use app\admin\model\Grade;
use app\admin\model\Clazz;
use app\admin\model\Workandrest;
use app\admin\model\Subject;
use app\admin\model\Semester;
use app\admin\model\SiteScene;

/**
 * 配置管理-控制器
 * @author 牧羊人
 * @since 2020/11/20
 * Class Config
 * @package app\admin\controller
 */
class Curriculum extends Backend
{
    /**
     * 初始化
     * @author 牧羊人
     * @since 2020/11/20
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\admin\model\Curriculum();
        $this->service = new CurriculumService();
    }

    /**
     * 获取周列表
     * @return mixed
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function getWeekList()
    {
        // 获取当前时间戳
        $currentTime = time();
        // 学期开始日期与结束日期
        $dandate = date('Y-m-d');//当前日期
        $type = input('type/d');
        $cid = input('cid/d');
        if ($type==1) {
            $gid = Clazz::where('id',$cid)->value('grade');
        }else{
            $gid = SiteScene::where('id',$cid)->value('grade');
        }
        if ($gid) {
            $sinfo = Semester::where('FIND_IN_SET(:gid, grade)',['gid'=>$gid])
            ->where('start','<=',$dandate)
            ->where('end','>=',$dandate)
            ->find();
            if (!$sinfo) {
                return $this->jsonReturn(message("请先配置学期", false));
            }
        }else{
            return $this->jsonReturn(message("请先配置学期", false));
        }
        

        $startTime = strtotime($sinfo['start']);
        $start_of_week = date('Y-m-d', strtotime('monday', $startTime));
        $end_of_week = date('Y-m-d', strtotime('sunday', $startTime));
        $endTime = strtotime($sinfo['end']);
        // 计算当前时间与学期开始时间的差值（以秒为单位）
        $weekStart = date('w',$startTime); //获取开始时间周几
        $weekEnd = date('w',$endTime); //获取结束时间周几
        //开始时间的一周结束计算一周
        $weekOne = (strtotime($end_of_week) - $startTime) / (24 * 3600);
        $startTime2 = $startTime+(($weekOne+1) * 24 * 3600);
        $endTime2 = $weekEnd==0?$endTime:$endTime-($weekEnd * 24 * 3600);

        $timeDifference = $endTime2 - $startTime2;
        // 获取当前时间所在周数
        $timeSuo = floor(($currentTime - strtotime($start_of_week)) / (7 * 24 * 3600))+1;
        // 将差值转换为周数
        $weeks = floor($timeDifference / (7 * 24 * 3600))+1;
        $list = [];
        $first = '';
        $datearr = '';
        $lableweek = [];
        $t = 0;
        // halt($timeSuo);
        for ($i=0; $i <$weeks ; $i++) { 
            $dataarr = [];
            if ($i==0) {
                $t++;
                $dataarr['numtxt'] = '第'.$t.'周';
                $dataarr['tabname'] = 'first'.$t;
                $dataarr['datearr'] = json_encode([date('Y-m-d', strtotime('monday this week', $startTime)),date('Y-m-d',$startTime+($weekOne * 24 * 3600))]);
                $list[] = $dataarr;
                if (($i+1)==$timeSuo) {
                    $first = 'first'.($t);
                    $datearr = $dataarr['datearr'];
                }
            }
            $t++;
            $dataarr['numtxt'] = '第'.$t.'周';
            $dataarr['tabname'] = 'first'.$t;
            $wstart = date('Y-m-d',$startTime2+($i * 7 * 24 * 3600));
            $wend = date('Y-m-d',strtotime('+6 day', $startTime2+($i * 7 * 24 * 3600)));
            $dataarr['datearr'] = json_encode([$wstart,$wend]); 
            $list[] = $dataarr;
            
            if (($i+1)==$timeSuo) {
                $first = 'first'.($t);
                $datearr = $dataarr['datearr'];
            }
        }
        //返回结果
        $result = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "first" => $first,
            "datearr" => $datearr
        );
        return $this->jsonReturn($result);
    }

    /**
     * 复制一周数据到下一周
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function copyoneweek()
    {
        // 参数
        $param = request()->param();
        $startEndTime = isset($param['start_end']) ? json_decode($param['start_end'],true) : [];
        $type = $param['type'];
        $cId = $param['cId'];
        $map = [];
        if ($type) {
            $map[] = ['type','=',$type];
        }
        if ($cId) {
            $map[] = ['tid','=',$cId];
        }
        if ($startEndTime && is_array($startEndTime)) {
            // 时间区间查询
            $map[] = ['course_date', 'between', [$startEndTime[0], $startEndTime[1]]];
        }
        
        if ($map) {
            $curriumModel = new \app\admin\model\Curriculum();
            $weeklist = $curriumModel->where($map)->select()->toArray();
            if ($weeklist) {
                foreach ($weeklist as $vv) {
                    $data = $vv;
                    unset($data['id']);
                    //一周后的日期
                    $data['course_date'] = date('Y-m-d',strtotime($vv['course_date'].' +7 day'));
                    $studyweekid = $curriumModel->where(['type'=>$type,'tid'=>$cId,'course_section'=>$data['course_section'],'course_date'=>$data['course_date']])->value('id');
                    if ($studyweekid) {
                        $data['id'] = $studyweekid;
                    }
                    $this->model->edit($data);
                }
                return $this->jsonReturn(message("复制成功"));
            }else{
                return $this->jsonReturn(message("没有可以复制的", false));
            }
        }else{
            return $this->jsonReturn(message("复制失败", false));
        }

    }

    /**
     * 复制一周数据到整个学期
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function copyoneweekxq()
    {
        // 参数
        $param = request()->param();
        $startEndTime = isset($param['start_end']) ? json_decode($param['start_end'],true) : [];
        $type = $param['type'];
        $cId = $param['cId'];
        $map = [];
        if ($type) {
            $map[] = ['type','=',$type];
        }
        if ($cId) {
            $map[] = ['tid','=',$cId];
        }
        if ($startEndTime && is_array($startEndTime)) {
            // 时间区间查询
            $map[] = ['course_date', 'between', [$startEndTime[0], $startEndTime[1]]];
        }
        
        if ($map) {
            $curriumModel = new \app\admin\model\Curriculum();
            $weeklist = $curriumModel->where($map)->select()->toArray();
            if ($weeklist) {
                $fzdate = $curriumModel->where($map)->value('course_date');
                $fzdate = date('Y-m-d',strtotime($fzdate.' +7 day'));
                $semester_id = $curriumModel->where($map)->value('semester_id');
                $end_date = Semester::where('id',$semester_id)->value('end');
                $interval = abs(strtotime($end_date) - strtotime($fzdate));
                $days = floor($interval / (60 * 60 * 24));
                $weeksDiff = (int)($days / 7);
                for ($i=1; $i < $weeksDiff; $i++) { 
                    foreach ($weeklist as $vv) {
                        $data = $vv;
                        //一周后的日期
                        $data['course_date'] = date('Y-m-d',strtotime($vv['course_date'].' +'.(7*$i).' day'));
                        $studyweekid = $curriumModel->where(['type'=>$type,'tid'=>$cId,'course_section'=>$data['course_section'],'course_date'=>$data['course_date']])->value('id');
                        if ($studyweekid) {
                            $data['id'] = $studyweekid;
                        }else{
                            unset($data['id']);
                        }
                        $this->model->edit($data);
                    }
                }
                
                return $this->jsonReturn(message("复制成功"));
            }else{
                return $this->jsonReturn(message("没有可以复制的", false));
            }
        }else{
            return $this->jsonReturn(message("复制失败", false));
        }

    }

    /**
     * 复制上一学期据到下一学期
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function copysemester()
    {
        // 参数
        $param = request()->param();
        $type = $param['stype'];
        $cId = $param['cId'];
        $start_date = $param['start_date'];
        $semester_fz_id = $param['semester_fz_id'];
        $semester_id = $param['semester_id'];
        if ($type) {
            $map[] = ['type','=',$type];
        }
        if ($cId) {
            $map[] = ['tid','=',$cId];
        }
        if ($semester_id==$semester_fz_id) {
            return $this->jsonReturn(message("请选择不同学期！", false));
        }
        $map = [];
        if ($semester_id) {
            $map[] = ['semester_id','=',$semester_fz_id];
        }
        
        if ($map) {
            $curriumModel = new \app\admin\model\Curriculum();
            $semesterlist = $curriumModel->where($map)->order('course_date asc')->select()->toArray();
            if ($semesterlist) {
                $end_date = Semester::where('id',$semester_fz_id)->value('start');
                if (strtotime($start_date)<=strtotime($end_date)) {
                    return $this->jsonReturn(message("所选学期日期大于开始日期", false));
                }
                $interval = abs(strtotime($start_date) - strtotime($end_date));
                $days = floor($interval / (60 * 60 * 24));
                foreach ($semesterlist as $vv) {
                    $data = $vv;
                    unset($data['id']);
                    unset($data['create_time']);
                    unset($data['update_user']);
                    unset($data['update_time']);
                    $data['semester_id'] = $semester_id;
                    //学期的日期
                    $data['course_date'] = date('Y-m-d',strtotime($vv['course_date'].' +'.$days.' day'));
                    $studysemesterid = $this->model->where($map)->where('course_section',$data['course_section'])->where('course_date',$data['course_date'])->value('id');
                    if ($studysemesterid) {
                        $data['id'] = $studysemesterid;
                    }
                    
                    $this->model->edit($data);
                }
                return $this->jsonReturn(message("复制成功"));
            }else{
                return $this->jsonReturn(message("没有可以复制的", false));
            }
        }else{
            return $this->jsonReturn(message("复制失败", false));
        }

    }

    /**
     * 真实删除数据
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function delete()
    {
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return $this->jsonReturn(message("记录ID不能为空", false));
        }
        if (is_array($ids)) {
            // 批量删除
            $result = $this->model->deleteDAll($ids,true);
            if (!$result) {
                return $this->jsonReturn(message("删除失败", false));
            }
            return $this->jsonReturn(message("删除成功"));
        } else {
            // 单个删除
            $info = $this->model->getInfo($ids);
            if ($info) {
                $result = $this->model->deleteDAll($ids,true);
                if ($result !== false) {
                    return $this->jsonReturn(message());
                }
            }
            return $this->jsonReturn(message($this->model->getError(), false));
        }
    }

    /**
     * 导入Excel
     * @author 牧羊人
     * @since 2021/5/24
     */
    public function importExcel()
    {
        $error = "";
        // 上传文件(非图片)
        $result = upload_file('file', '', $error);
        if (!$result) {
            return $this->jsonReturn(message($error, false));
        }
        // 文件路径
        $filePath = UPLOAD_TEMP_PATH . $result;
        if (!file_exists($filePath)) {
            return $this->jsonReturn(message("文件不存在", false));
        }
        // 读取文件
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath); //获取sheet表格数目
        $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected); //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow(); //获取表格列数
        $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        // 计数器
        $totalNum = 0;
        // 循环读取行数据
        for ($row = 2; $row <= $rowCount; $row++) {
            // 获取列值
            $dataArr = array();
            // 类型
            $type = $objPHPExcel->getActiveSheet()->getCell("A" . $row)->getValue();
            $dataArr['type'] = $type=='班级'?1:2;
            // 所属班级或场地
            $csbjcd = $objPHPExcel->getActiveSheet()->getCell("B" . $row)->getValue();
            $csarr = explode('/', $csbjcd);
            // halt($csarr);
            $sc_id = School::where(['name'=>trim($csarr[0]),'mark'=>1])->value('id');//学校id
            $grade_id = Grade::where(['organization'=>$sc_id,'name'=>trim($csarr[1]),'mark'=>1])->value('id');//年级id
            if ($dataArr['type']==1) {
                $dataArr['tid'] = Clazz::where(['grade'=>$grade_id,'name'=>trim($csarr[2]),'mark'=>1])->value('id');//班级id
            }else{
                $dataArr['tid'] = SiteScene::where(['grade'=>$grade_id,'name'=>trim($csarr[2]),'mark'=>1])->value('id');//场地id
            }
            // 课程名称
            $course = $objPHPExcel->getActiveSheet()->getCell("C" . $row)->getValue();
            $dataArr['course_id'] = Subject::where(['organization'=>$sc_id,'name'=>trim($course),'mark'=>1])->value('id')?:null;
            // 课程老师
            $teacher = $objPHPExcel->getActiveSheet()->getCell("D" . $row)->getValue();
            $dataArr['teacher_id'] = User::where(['school_id'=>$sc_id,'realname'=>trim($teacher),'mark'=>1])->value('id')?:null;
            // 课程日期
            $cdate = $objPHPExcel->getActiveSheet()->getCell("E" . $row)->getValue();
            //所属学期
            $dataArr['semester_id'] = Semester::where('FIND_IN_SET(:gid, grade)',['gid'=>$grade_id])->where('start','<=',$cdate)->where('end','>=',$cdate)->value('id')?:null;
            //所属作息时间
            $dataArr['work_id'] = Workandrest::where(['grade'=>$grade_id,'semester'=>$dataArr['semester_id']])->value('id')?:null;
            $dataArr['course_date'] = $cdate;
            // 节次
            $dataArr['course_section'] = $objPHPExcel->getActiveSheet()->getCell("F" . $row)->getValue();
           
            $ids = $this->model->where(['type'=>$dataArr['type'],'semester_id'=>$dataArr['semester_id'],'tid'=>$dataArr['tid'],'work_id'=>$dataArr['work_id'],'course_date'=>$dataArr['course_date'],'course_section'=>$dataArr['course_section']])->value('id');
            if ($ids) {
               $dataArr['id'] = $ids;
            }
            // 插入数据
            if($dataArr['course_id']){
                $result = $this->model->edit($dataArr);
                if ($result) {
                    $totalNum++;
                }
            }
            
        }
        return $this->jsonReturn(message("本次共导入{$totalNum}条数据", true));
    }

    /**
     * 导出Excel
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     * @since 2021/3/25
     * @author 牧羊人
     */
    public function exportExcel()
    {
        // 参数
        $param = request()->param();
        // 查询条件
        $map = [];

        // 类型
        $type = isset($param['type']) ? (int)$param['type'] : 0;
        if ($type) {
            $map[] = ['type', '=', $type];
        }

        // id
        $cId = isset($param['cId']) ? (int)$param['cId'] : 0;
        if ($cId) {
            $map[] = ['cId', '=', $cId];
        }

        // 更新时间
        $startEndTime = isset($param['start_end']) ? json_decode($param['start_end'],true) : [];
        if ($startEndTime && is_array($startEndTime)) {
            // 时间区间查询
            $map[] = ['course_date', 'between', [$startEndTime[0], $startEndTime[1]]];
        }
        $map[] = ['mark', "=", 1];
        $dandate = date('Y-m-d');//当前日期
        if ($type==1) {
            $gid = Clazz::where('id',$cId)->value('grade');
        }else{
            $gid = SiteScene::where('id',$cId)->value('grade');
        }
        if ($gid) {
            $sid = Semester::where('FIND_IN_SET(:gid, grade)',['gid'=>$gid])
            ->where('start','<=',$dandate)
            ->where('end','>=',$dandate)
            ->value('id');
            $wid = Workandrest::where(['grade'=>$gid,'semester'=>$sid])->value('id');
            if (!$wid) {
                return message("请先配置作息时间", false);
            }
        }else{
            return message("请先配置学期", false);
        }
        $workdata = json_decode(Workandrest::where(['id'=>$wid])->value('data'),true);
        $data = [];
        $item = [];
        $t = 0;
        if(isset($workdata['zaozi'])&&$workdata['zaozi']==1){
            $t++;
            $item = $this->service->startcourseEnd($startEndTime[0],$startEndTime[1],$type,$cId,$t,$sid,$wid);
            $item['jie'] = '第'.$t.'节(早自习)';
            $item['jienum'] = $t;
            $item['time'] = implode('~', $workdata['zaozitime']);
            $data[] = $item;
        }
        if(isset($workdata['shang'])&&$workdata['shang']){
            $shang = $workdata['shang'];
            foreach ($shang as $val) {
                $t++;
                $item = $this->service->startcourseEnd($startEndTime[0],$startEndTime[1],$type,$cId,$t,$sid,$wid);
                $item['jie'] = '第'.$t.'节';
                $item['jienum'] = $t;
                $item['time'] = implode('~', $val);
                $data[] = $item;
            }
        }
        if(isset($workdata['xia'])&&$workdata['xia']){
            $xia = $workdata['xia'];
            foreach ($xia as $val) {
                $t++;
                $item = $this->service->startcourseEnd($startEndTime[0],$startEndTime[1],$type,$cId,$t,$sid,$wid);
                $item['jie'] = '第'.$t.'节';
                $item['jienum'] = $t;
                $item['time'] = implode('~', $val);
                $data[] = $item;
            }
        }
        if(isset($workdata['wan'])&&$workdata['wan']){
            $wan = $workdata['wan'];
            foreach ($wan as $val) {
                $t++;
                $item = $this->service->startcourseEnd($startEndTime[0],$startEndTime[1],$type,$cId,$t,$sid,$wid);
                $item['jie'] = '第'.$t.'节';
                $item['jienum'] = $t;
                $item['time'] = implode('~', $val);
                $data[] = $item;
            }
        }
        if(isset($workdata['wanzi'])&&$workdata['wanzi']==1){
            $t++;
            $item = $this->service->startcourseEnd($startEndTime[0],$startEndTime[1],$type,$cId,$t,$sid,$wid);
            $item['jie'] = '第'.$t.'节(晚自习)';
            $item['jienum'] = $t;
            $item['time'] = implode('~', $workdata['wanzitime']);
            $data[] = $item;
        }
        $labelweek = $this->service->startdateEnd($startEndTime[0],$startEndTime[1]);

        // 实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        // 激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '节次')
            ->setCellValue('B1', '时间')
            ->setCellValue('C1', $labelweek['zhou1'])
            ->setCellValue('D1', $labelweek['zhou2'])
            ->setCellValue('E1', $labelweek['zhou3'])
            ->setCellValue('F1', $labelweek['zhou4'])
            ->setCellValue('G1', $labelweek['zhou5'])
            ->setCellValue('H1', $labelweek['zhou6'])
            ->setCellValue('I1', $labelweek['zhou7']);
        // 设置表格头水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(10);
        // 循环刚取出来的数组，将数据逐一添加到excel表格。
        for ($i = 0; $i < count($data); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $data[$i]['jie']);// 节次
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $data[$i]['time']);// 时间
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $data[$i]['zhou1']?$data[$i]['zhou1'][0].','.$data[$i]['zhou1'][1]:'');// 周一
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $data[$i]['zhou2']?$data[$i]['zhou2'][0].','.$data[$i]['zhou2'][1]:'');// 周二
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $data[$i]['zhou3']?$data[$i]['zhou3'][0].','.$data[$i]['zhou3'][1]:'');// 周三
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $data[$i]['zhou4']?$data[$i]['zhou4'][0].','.$data[$i]['zhou4'][1]:'');// 周四
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $data[$i]['zhou5']?$data[$i]['zhou5'][0].','.$data[$i]['zhou5'][1]:'');// 周五
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $data[$i]['zhou6']?$data[$i]['zhou6'][0].','.$data[$i]['zhou6'][1]:'');// 周六
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $data[$i]['zhou7']?$data[$i]['zhou7'][0].','.$data[$i]['zhou7'][1]:'');// 周日
        }
        // 设置保存的Excel表格名称
        $filename = '课表管理_'.$startEndTime[0].'-'.$startEndTime[1] . date('YmdHis', time()) . ".xlsx";
        // 设置当前激活的sheet表格名称
        $objPHPExcel->getActiveSheet()->setTitle('课表管理');

        // 保存本地文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(UPLOAD_TEMP_PATH . "/" . $filename);
        return download(UPLOAD_TEMP_PATH . "/" . $filename, $filename);
        // 文件地址
       // $filePath = get_image_url('/uploads'.str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH . "/" . $filename));
       // return $this->jsonReturn(message("操作成功", true, $filePath));
    }
}